package com.ita.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ita.dao.FoodDao;
import com.ita.pojo.Food;

@Repository(value = "foodDao")
public class FoodDaoImpl extends BaseDaoImpl<Food> implements FoodDao {

	@Override
	public List<Food> loadFood(String type) {
		String jpql = "from Food f where f.type.id =:type";
		return em.createQuery(jpql).setParameter("type", type).getResultList();

	}

//	@Override
//	@Transactional
//	public int deleteFood(String id) {
//		String jpql = "delete from Food f where f.id=:id";
//		return em.createQuery(jpql).setParameter("id", id).getFirstResult();
//	}

	@Override
	public List<Food> findAll(String m_id) {
		String jpql = "from Food f where f.merchant.id =:mid";
		return em.createQuery(jpql).setParameter("mid", m_id).getResultList();
	}

	@Override
//	@Transactional
	public Food update(Food f) {

		String jpql = "update Food f set f.name=:name,f.img=:img,f.price=:price,f.type=:type where f.id=:id";

		Query query = em.createQuery(jpql);
		query.setParameter("id", f.getId());
		query.setParameter("name", f.getName());
		query.setParameter("price", f.getPrice());
		query.setParameter("img", f.getImg());
		query.setParameter("type", f.getType());
		query.executeUpdate();

		return f;
	}

}
